from flask import Flask
from core.scheduler import schedule
from core.prometheus import setup_prometheus
from api.tasks import tasks_bp
from api.config import config_bp
from utils.logger import setup_logging
from models.db import MGClient
import os


app = Flask(__name__)
app.json.ensure_ascii = False
setup_logging()

# 初始化监控
setup_prometheus(app)

# 注册蓝图
app.register_blueprint(tasks_bp, url_prefix='/api/v1')
app.register_blueprint(config_bp, url_prefix='/api/v1')

if __name__ == '__main__':
    # 启动任务调度器，debug模式必须为False，或默认
    try:
        print(f"当前进程id：{os.getpid()}")
        schedule.start()
        app.run(port=5000)
    finally:
        MGClient.close()
